package com.lenovo.mso2o.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.entity.g.AutoSignOrder;
import com.lenovo.mso2o.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/3/5.
 */
public class AutoSignAdapter extends RecyclerView.Adapter<AutoSignAdapter.ViewHolder> {
    private List<AutoSignOrder> autoSignList;
    private Context mContext;
    private LatLng latLng;
    private SignRecyclerItemClickListener listener = null;
    private WindowManager wm;
    public AutoSignAdapter(List<AutoSignOrder> autoSignList_, Context context){
        this.mContext = context;
        this.wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(autoSignList_ == null){
            autoSignList = new ArrayList<>();
        }else{
            autoSignList = autoSignList_;
        }
    }

    public void setmOnItemClickListener(SignRecyclerItemClickListener listener_){
        listener = listener_;
    }
    @Override
    public AutoSignAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_auto_sign, parent, false);
        ViewGroup.LayoutParams params = v.getLayoutParams();
        int height = wm.getDefaultDisplay().getHeight();
        params.height = height/8;
        v.setLayoutParams(params);
        return new ViewHolder(v);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(AutoSignAdapter.ViewHolder holder, int position) {
        AutoSignOrder item = autoSignList.get(position);
        holder.title.setText(item.getTitle());
        holder.ticketID.setText("工单编号：" + item.getTicketid());
        holder.project.setText("项目名称：" + item.getProject());
        /*if(latLng != null) {
            //double lon1, double lat1, double lon2, double lat2
            String dis = MapDistance.getInstance().getShortDistance(latLng.longitude, latLng.latitude, item.location.longitude, item.location.latitude);
            holder.distance.setText(dis);
        }else{
            holder.distance.setText("");
        }*/
    }

    @Override
    public int getItemCount() {
        return autoSignList.size();
    }

    //define interface
    public static interface SignRecyclerItemClickListener {
        void onItemClick(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView ticketID;
        public TextView project;

        public ViewHolder(final View rootView) {
            super(rootView);
            title = (TextView) rootView.findViewById(R.id.title_auto_sign_item);
            ticketID = (TextView) rootView.findViewById(R.id.ticketid_auto_sign_item);
            project = (TextView) rootView.findViewById(R.id.project_auto_sign_item);
            ViewGroup.LayoutParams paramsTitle = title.getLayoutParams();
            int height = wm.getDefaultDisplay().getHeight();
            paramsTitle.height = height/32;
            title.setLayoutParams(paramsTitle);

            ViewGroup.LayoutParams paramsTicket = ticketID.getLayoutParams();
            paramsTicket.height = height/32;
            title.setLayoutParams(paramsTicket);

            ViewGroup.LayoutParams paramsProject = title.getLayoutParams();
            paramsProject.height = height/32;
            title.setLayoutParams(paramsProject);

            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getLayoutPosition();
                    if (listener != null && pos != -1) {
                        listener.onItemClick(pos);
                    }
                }
            });
        }
    }
}
